package com.base.myproject.client.HR.attendance;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseCheckGroup;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * 考勤明细表
 * @author Administrator
 *
 */
public class Att_Rep_Details extends BaseJDBCPanel{
	String tree_depcode = null;// 讲当前树形结构中选中的部门id保存
	String tree_de_name = null;// 讲当前树形结构中选中的部门name保存

	TabPanel tabPanel = null;
	TreePanel<TreeNodeModelData> treePanel = null;
	// 创建树形结构的数据表
	BaseTreeStore<TreeNodeModelData> treeStore = null;
	
	//BaseComboBoxX dep 		= new BaseComboBoxX("name", "SystemInfo"," where len(id)>4");
	DateField seach_by_date = new DateField();//查询日期
	Button seach_bt	= new Button("查询");//查询按钮
	
	private String bcode = Env.getEnvbcode();
	private String results = null;
	private BaseCheckGroup check = null;
	private List<String> level = null;
	
	public Att_Rep_Details() {
		String sql = "select substring('id like '''+replace(permissionsbcode,';','%'' or id like '''),9," +
		"len('id like '''+replace(permissionsbcode,';','%'' or id like '''))-21) as result from" +
		" busipermissions where bcode = '"+bcode+"' and busicode = 8000";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sql, new AsyncCallback<DataSet>(){
		@Override
			public void onFailure(Throwable caught) {	
			}
			@Override
			public void onSuccess(DataSet result) {  
				if(result.getRowCount() != 0){
					results = result.getValue(0, "result");
					String sql2 = "select name,type from dictionary where type='zy'";
					final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT.create(com.base.myproject.client.tools.GreetingService.class);
					greetingService2.getDataSet(sql2, new AsyncCallback<DataSet>(){
					@Override
						public void onFailure(Throwable caught) {	
						}
						@Override
						public void onSuccess(DataSet result) {  
							if(result.getRowCount() != 0){
								level = new ArrayList<String>();
								for(int i = 0;i < result.getRowCount();i++){
									level.add(result.getValue(i, "name"));
								}
							}
							init();
						}
					});
				}else{
					MessageBox.alert("提示", "抱歉，您没有查看权限", null);
				}
			}
		});
	} 
	
	public void init() {
		initWeigetx();
	} 
	
	public void complete() {
		super.complete();
		print.addListener(Events.Select, getListener());
		getToolbarll().add(print);
	}
	public void initWeigetx() {
		
		final BorderLayout layout = new BorderLayout();  
		this.setLayout(layout);
		//水平布局
		HorizontalPanel cp=new HorizontalPanel();
		cp.add(new Label("查询日期:"));
		seach_by_date.setPropertyEditor(new DateTimePropertyEditor("yyyy-MM"));
		cp.add(seach_by_date);
		
		String title[] = level.toArray(new String[level.size()]);
		check = new BaseCheckGroup(title,true);
		check.setHeight(30);
		cp.add(new Label("雇佣级别:"));
		cp.add(check);
		
		seach_bt.addListener(Events.OnClick, getListener());
		cp.add(seach_bt);
		
		//垂直布局
		VerticalPanel vp=new VerticalPanel();
		vp.setSpacing(5);
		vp.add(cp);
		vp.add(getBaseGrid());
		
		ContentPanel center = new ContentPanel();
		center.setHeaderVisible(false);
		center.add(vp);
		
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,200);
		westData.setCollapsible(true);
		westData.setFloatable(true);
		westData.setHideCollapseTool(true);
		westData.setSplit(true);
		westData.setMargins(new Margins(5, 5, 0, 0));

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		
		ContentPanel panel = new ContentPanel();
		panel.setHeading("选择部门");
		panel.setCollapsible(true);
		panel.add(getTreePanel());
		
		add(panel, westData);
		add(center, centerData);
		
		this.layout();
		
	}
	// 创建数据表
	BaseStore<BusiObject> store = null;
	BaseStore<BusiObject> getStore() {//String bcode
		if (store == null) {
			createStore();//bcode
		}
		return store;
	}
	/*************tree*********/
	TreePanel<TreeNodeModelData> getTreePanel() {
		if (treePanel == null) {
			initTreePanel();
		}
		return treePanel;
	}
	BaseTreeStore<TreeNodeModelData> getTreeStore() {
		if (treeStore == null) {
			initTreeStore();
		}
		return treeStore;
	}
	// 创建数据连接表
	BaseTreeStore<TreeNodeModelData> initTreeStore() {
		StoreInfo si = new StoreInfo();
		si
				.setSql("select icode,id,name from SystemInfo where id like "+results+" order by id asc");
		si.setTreesortcolumn("id");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("SystemInfo");
		treeStore = new BaseTreeStore<TreeNodeModelData>(si, false);
		treeStore.load();
		return treeStore;

	}
	// 实例化TreePanel
	TreePanelSelectionModel<TreeNodeModelData> tpsm = new TreePanelSelectionModel<TreeNodeModelData>();

	void initTreePanel() {
		// super.onRender();
		treePanel = new TreePanel<TreeNodeModelData>(getTreeStore());
		treePanel.setWidth(200);
		treePanel.setHeight(445);
		treePanel.setDisplayProperty("name");
		treePanel.setSelectionModel(tpsm);
		treePanel.setCheckStyle(CheckCascade.PARENTS);
		treePanel.setCheckNodes(CheckNodes.BOTH);
		treePanel.getStyle().setLeafIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.tree_file())); 
		treePanel.setCheckable(false);
		treePanel.setAutoLoad(true);

		treePanel.addListener(Events.OnClick,
				new Listener<TreePanelEvent<TreeNodeModelData>>() {
					public void handleEvent(TreePanelEvent<TreeNodeModelData> be) {
						String id = be.getItem().get("id").toString();
						//if (!id.equals("00")) {// 表示为主节点 不因有任何操作
							//set_grid(be);
						//}
						tree_depcode = id;// 讲当前树形结构中选中的部门id保存
						tree_de_name = be.getItem().get("name").toString();
					}
				});
	}
	/**********************************/
	void createStore() {//String bcode
		ModelType type_m = new ModelType();
		type_m.setRoot("records");
		type_m.setRecordName("record");
		type_m.setTotalName("totle");

		type_m.addField("bcode");
		type_m.getField("bcode").setType(String.class);
		type_m.addField("bname");
		type_m.getField("bname").setType(String.class);

		
		
		type_m.addField("r1");
		type_m.getField("r1").setType(String.class);
		type_m.addField("r2");
		type_m.getField("r2").setType(String.class);
		type_m.addField("r3");
		type_m.getField("r3").setType(String.class);
		type_m.addField("r4");
		type_m.getField("r4").setType(String.class);
		type_m.addField("r5");
		type_m.getField("r5").setType(String.class);
		type_m.addField("r6");
		type_m.getField("r6").setType(String.class);
		type_m.addField("r7");
		type_m.getField("r7").setType(String.class);
		type_m.addField("r8");
		type_m.getField("r8").setType(String.class);
		type_m.addField("r9");
		type_m.getField("r9").setType(String.class);
		type_m.addField("r10");
		type_m.getField("r10").setType(String.class);
		type_m.addField("r11");
		type_m.getField("r11").setType(String.class);
		type_m.addField("r12");
		type_m.getField("r12").setType(String.class);
		type_m.addField("r13");
		type_m.getField("r13").setType(String.class);
		type_m.addField("r14");
		type_m.getField("r14").setType(String.class);
		type_m.addField("r15");
		type_m.getField("r15").setType(String.class);
		type_m.addField("r16");
		type_m.getField("r16").setType(String.class);
		type_m.addField("r17");
		type_m.getField("r17").setType(String.class);
		type_m.addField("r18");
		type_m.getField("r18").setType(String.class);
		type_m.addField("r19");
		type_m.getField("r19").setType(String.class);
		type_m.addField("r20");
		type_m.getField("r20").setType(String.class);
		type_m.addField("r21");
		type_m.getField("r21").setType(String.class);
		type_m.addField("r22");
		type_m.getField("r22").setType(String.class);
		type_m.addField("r23");
		type_m.getField("r23").setType(String.class);
		type_m.addField("r24");
		type_m.getField("r24").setType(String.class);
		type_m.addField("r25");
		type_m.getField("r25").setType(String.class);
		type_m.addField("r26");
		type_m.getField("r26").setType(String.class);
		type_m.addField("r27");
		type_m.getField("r27").setType(String.class);
		type_m.addField("r28");
		type_m.getField("r28").setType(String.class);
		type_m.addField("r29");
		type_m.getField("r29").setType(String.class);
		type_m.addField("r30");
		type_m.getField("r30").setType(String.class);
		type_m.addField("r31");
		type_m.getField("r31").setType(String.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_schedule");
		info.setKeyname(new String[] { "icode" });
		//bname,bcode,sign,ym,depCode,icode,
		info.setSql("select *  from hr_Attendance_schedule where 1=2");
	//	info.setSql("select bname,bcode,sign,ym,depCode,icode, isnull(r1,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-1' and department='核数' and bcode=hr_schedule.bcode) as r1,isnull(r2,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-2' and department='核数' and bcode=hr_schedule.bcode) as r2,isnull(r3,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-3' and department='核数' and bcode=hr_schedule.bcode) as r3,isnull(r4,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-4' and department='核数' and bcode=hr_schedule.bcode) as r4,isnull(r5,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-5' and department='核数' and bcode=hr_schedule.bcode) as r5,isnull(r6,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-6' and department='核数' and bcode=hr_schedule.bcode) as r6,isnull(r7,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-7' and department='核数' and bcode=hr_schedule.bcode) as r7,isnull(r8,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-8' and department='核数' and bcode=hr_schedule.bcode) as r8,isnull(r9,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-9' and department='核数' and bcode=hr_schedule.bcode) as r9,isnull(r10,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-10' and department='核数' and bcode=hr_schedule.bcode) as r10,isnull(r11,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-11' and department='核数' and bcode=hr_schedule.bcode) as r11,isnull(r12,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-12' and department='核数' and bcode=hr_schedule.bcode) as r12,isnull(r13,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-13' and department='核数' and bcode=hr_schedule.bcode) as r13,isnull(r14,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-14' and department='核数' and bcode=hr_schedule.bcode) as r14,isnull(r15,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-15' and department='核数' and bcode=hr_schedule.bcode) as r15,isnull(r16,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-16' and department='核数' and bcode=hr_schedule.bcode) as r16,isnull(r17,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-17' and department='核数' and bcode=hr_schedule.bcode) as r17,isnull(r18,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-18' and department='核数' and bcode=hr_schedule.bcode) as r18,isnull(r19,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-19' and department='核数' and bcode=hr_schedule.bcode) as r19,isnull(r20,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-20' and department='核数' and bcode=hr_schedule.bcode) as r20,isnull(r21,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-21' and department='核数' and bcode=hr_schedule.bcode) as r21,isnull(r22,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-22' and department='核数' and bcode=hr_schedule.bcode) as r22,isnull(r23,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-23' and department='核数' and bcode=hr_schedule.bcode) as r23,isnull(r24,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-24' and department='核数' and bcode=hr_schedule.bcode) as r24,isnull(r25,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-25' and department='核数' and bcode=hr_schedule.bcode) as r25,isnull(r26,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-26' and department='核数' and bcode=hr_schedule.bcode) as r26,isnull(r27,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-27' and department='核数' and bcode=hr_schedule.bcode) as r27,isnull(r28,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-28' and department='核数' and bcode=hr_schedule.bcode) as r28,isnull(r29,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-29' and department='核数' and bcode=hr_schedule.bcode) as r29,isnull(r30,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-30' and department='核数' and bcode=hr_schedule.bcode) as r30,isnull(r31,'') %2B (select value=MIN(value) %2B CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-31' and department='核数' and bcode=hr_schedule.bcode) as r31 from hr_schedule where CONVERT(varchar(7),ym,120)='2010-07' and depCode in (select id from SystemInfo where name='核数')action=Loaddatae=tableinfsql=select bname,bcode, isnull(r1,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-1' and department='核数' and bcode=hr_schedule.bcode) as r1,isnull(r2,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-2' and department='核数' and bcode=hr_schedule.bcode) as r2,isnull(r3,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-3' and department='核数' and bcode=hr_schedule.bcode) as r3,isnull(r4,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-4' and department='核数' and bcode=hr_schedule.bcode) as r4,isnull(r5,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-5' and department='核数' and bcode=hr_schedule.bcode) as r5,isnull(r6,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-6' and department='核数' and bcode=hr_schedule.bcode) as r6,isnull(r7,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-7' and department='核数' and bcode=hr_schedule.bcode) as r7,isnull(r8,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-8' and department='核数' and bcode=hr_schedule.bcode) as r8,isnull(r9,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-9' and department='核数' and bcode=hr_schedule.bcode) as r9,isnull(r10,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-10' and department='核数' and bcode=hr_schedule.bcode) as r10,isnull(r11,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-11' and department='核数' and bcode=hr_schedule.bcode) as r11,isnull(r12,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-12' and department='核数' and bcode=hr_schedule.bcode) as r12,isnull(r13,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-13' and department='核数' and bcode=hr_schedule.bcode) as r13,isnull(r14,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-14' and department='核数' and bcode=hr_schedule.bcode) as r14,isnull(r15,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-15' and department='核数' and bcode=hr_schedule.bcode) as r15,isnull(r16,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-16' and department='核数' and bcode=hr_schedule.bcode) as r16,isnull(r17,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-17' and department='核数' and bcode=hr_schedule.bcode) as r17,isnull(r18,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-18' and department='核数' and bcode=hr_schedule.bcode) as r18,isnull(r19,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-19' and department='核数' and bcode=hr_schedule.bcode) as r19,isnull(r20,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-20' and department='核数' and bcode=hr_schedule.bcode) as r20,isnull(r21,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-21' and department='核数' and bcode=hr_schedule.bcode) as r21,isnull(r22,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-22' and department='核数' and bcode=hr_schedule.bcode) as r22,isnull(r23,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-23' and department='核数' and bcode=hr_schedule.bcode) as r23,isnull(r24,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-24' and department='核数' and bcode=hr_schedule.bcode) as r24,isnull(r25,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-25' and department='核数' and bcode=hr_schedule.bcode) as r25,isnull(r26,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-26' and department='核数' and bcode=hr_schedule.bcode) as r26,isnull(r27,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-27' and department='核数' and bcode=hr_schedule.bcode) as r27,isnull(r28,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-28' and department='核数' and bcode=hr_schedule.bcode) as r28,isnull(r29,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-29' and department='核数' and bcode=hr_schedule.bcode) as r29,isnull(r30,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-30' and department='核数' and bcode=hr_schedule.bcode) as r30,isnull(r31,'') + (select value=MIN(value) + CASE  WHEN COUNT(*)=1 THEN '' ELSE MAX(value) END from v_ot where useDate='2010-07-31' and department='核数' and bcode=hr_schedule.bcode) as r31 from hr_schedule where CONVERT(varchar(7),ym,120)='2010-07' and depCode in (select id from SystemInfo where name='核数')");
		store = new BaseStore<BusiObject>(type_m, info);	
	}
	// 创建一个grid
	BaseGrid<BusiObject> grid = null;
	BaseGrid<BusiObject> getBaseGrid() {
		if (grid == null) {
			createGrid();
		}
		return grid;
	}
	/**
	 * 为排班表定义31个text变量
	 */
	private ColumnConfig C34 = null;
	private ColumnConfig C35 = null;
	private ColumnConfig C36 = null;
	void createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		
		ColumnConfig C4 = new ColumnConfig("bcode", "员工编号", 75);
		columns.add(C4);
		
		ColumnConfig C3 = new ColumnConfig("bname", "姓名", 65);
		columns.add(C3);
		
		int cw = 30;
		ColumnConfig C6 = new ColumnConfig("r1", "1", cw);
		columns.add(C6);
		
		ColumnConfig C7 = new ColumnConfig("r2", "2", cw);
		columns.add(C7);
		
		ColumnConfig C8 = new ColumnConfig("r3", "3", cw);
		columns.add(C8);
		
		ColumnConfig C9 = new ColumnConfig("r4", "4", cw);
		columns.add(C9);
		
		
		ColumnConfig C10 = new ColumnConfig("r5", "5", cw);
		columns.add(C10);
		
		ColumnConfig C11 = new ColumnConfig("r6", "6", cw);
		columns.add(C11);
		
		ColumnConfig C12 = new ColumnConfig("r7", "7", cw);
		columns.add(C12);
		
		ColumnConfig C13 = new ColumnConfig("r8", "8", cw);
		columns.add(C13);
		
		ColumnConfig C14 = new ColumnConfig("r9", "9", cw);
		columns.add(C14);
		
		ColumnConfig C15 = new ColumnConfig("r10", "10", cw);
		columns.add(C15);
		
		ColumnConfig C16 = new ColumnConfig("r11", "11", cw);
		columns.add(C16);
		
		ColumnConfig C17 = new ColumnConfig("r12", "12", cw);
		columns.add(C17);
		
		ColumnConfig C18 = new ColumnConfig("r13", "13", cw);
		columns.add(C18);
		
		ColumnConfig C19 = new ColumnConfig("r14", "14", cw);
		columns.add(C19);
		
		ColumnConfig C20 = new ColumnConfig("r15", "15", cw);
		columns.add(C20);
		
		ColumnConfig C21 = new ColumnConfig("r16", "16", cw);
		columns.add(C21);
		
		ColumnConfig C22 = new ColumnConfig("r17", "17", cw);
		columns.add(C22);
		
		ColumnConfig C23 = new ColumnConfig("r18", "18", cw);
		columns.add(C23);
		
		ColumnConfig C24 = new ColumnConfig("r19", "19", cw);
		columns.add(C24);
		
		ColumnConfig C25 = new ColumnConfig("r20", "20", cw);
		columns.add(C25);
		
		ColumnConfig C26 = new ColumnConfig("r21", "21", cw);
		columns.add(C26);
		
		ColumnConfig C27 = new ColumnConfig("r22", "22", cw);
		columns.add(C27);
		
		ColumnConfig C28 = new ColumnConfig("r23", "23", cw);
		columns.add(C28);
		
		ColumnConfig C29 = new ColumnConfig("r24", "24", cw);
		columns.add(C29);
		
		ColumnConfig C30 = new ColumnConfig("r25", "25", cw);
		columns.add(C30);
		
		ColumnConfig C31 = new ColumnConfig("r26", "26", cw);
		columns.add(C31);
		
		ColumnConfig C32 = new ColumnConfig("r27", "27", cw);
		columns.add(C32);
		
		ColumnConfig C33 = new ColumnConfig("r28", "28", cw);
		columns.add(C33);
		
		C34 = new ColumnConfig("r29", "29", cw);
		columns.add(C34);
		
		C35 = new ColumnConfig("r30", "30", cw);
		columns.add(C35);
		
		C36 = new ColumnConfig("r31", "31", cw);
		columns.add(C36);
		
		ColumnModel cm = new ColumnModel(columns);
	
		grid = new BaseGrid<BusiObject>(getStore(), cm ,false,false);
		cbs.setSelectionMode(SelectionMode.SINGLE);//控制是否可以多选每行后面的复选框
		grid.setTitle("排班信息");
		grid.setSelectionModel(cbs);
		Tools.setResize(grid,0.99,0.94);
	}
	public void to_seach(String level){
		String sql = "select bname,(select employno from bcode_job where a.bcode=bcode_job.bcode and employstatus='在职') as bcode, ";
		String s_date = seach_by_date.getRawValue();
		String dep_code = tree_depcode;
		int year=Integer.parseInt(s_date.substring(0, 4));
		int month=Integer.parseInt(s_date.substring(5, 7));
		int m_day=days(year,month);//得到指定月份的天数
		if(m_day == 28 ){
			C34.setHidden(true);
			C35.setHidden(true);
			C36.setHidden(true);
		}else if(m_day == 29){
			C35.setHidden(true);
			C36.setHidden(true);
		}else if(m_day == 30){
			C36.setHidden(true);
		}else{
			C34.setHidden(false);
			C35.setHidden(false);
			C36.setHidden(false);
		}
		for(int i=1;i<m_day+1;i++){
			String d="";
			if(i<10){
				d="0"+String.valueOf(i);
			}else{
				d=String.valueOf(i);
			}
			
			sql += "isnull(r"+i+",'') %2B isnull((select stuff((select '' %2B value from v_ot where type in ('加班','补休') and bcode=a.bcode and depCode=a.depCode and usedate='"+s_date+"-"+d+"' " +
				   " for xml path('')),1,0,'')),'') as r"+i+",";
			
		}
		sql=sql.substring(0, sql.length()-1);
		sql += " from hr_attendance_schedule as a where depcode like '"+ dep_code 
				+"%25' and convert(varchar(7),ym,120)='" + s_date + "'"; 
		if (!level.equals("")) {
			sql += " and exists (select * from bcode_job where bcode = a.bcode and employlevel in("+level+") and employstatus='在职')";
		}
		
		grid.getStore().getStoreinfo().setSql(sql);
		grid.getStore().reload();
	}
	
//	public void print(){
//		String s_date=seach_by_date.getRawValue();
//		String dep_code=tree_depcode;
//		int year=Integer.parseInt(s_date.substring(0, 4));
//		int month=Integer.parseInt(s_date.substring(5, 7));
//		ReportPrintDialog rpd = new ReportPrintDialog();
//		rpd.setJspUrl("RepDetails");
//		String data = s_date + "_" + dep_code + "_" + year + "_" + month;
//		rpd.setData(data);
//		rpd.show();
//	}
	
	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						String level = "";
						if(check.getValueArray().length != 0){
							for(int i = 0;i < check.getValueArray().length;i++){
								String value = (String)check.getValueArray()[i];
								level += "'" + value + "'" + ",";
							}
							level = level.substring(0, level.length() - 1);
						}
						
						if (be.getType() == Events.Select) {
							if(be.getSource().equals(print)){
								if(tree_depcode==null || seach_by_date.getRawValue().equals("")){
									MessageBox.alert("警告", "请选择查询条件",null);
								}else{
									String s_date = seach_by_date.getRawValue();
									String dep_code = tree_depcode;
									int year=Integer.parseInt(s_date.substring(0, 4));
									int month=Integer.parseInt(s_date.substring(5, 7));
									String data = s_date + "_" + dep_code + "_" + year + "_" + month + "&level=" + level;
									Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/rptRepDetails.jsp?data=" + URL.encode(data),"_blank","menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
								}
							}
						}  else if (be.getType() == Events.OnClick) {
							if (be.getSource().equals(seach_bt)) {
								if(tree_depcode==null || seach_by_date.getRawValue().equals("")){
									MessageBox.alert("警告", "请选择查询条件",null);
								}else{
									to_seach(level);
								}
								
							}
						} else if (be.getType() == Events.Change) {
							
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}	
	/*
	 * 得到指定年月的 天数
	 */
	public static int days(int year,int month){
	   int days = 0;
	   if(month!=2) {
		   switch(month) {
		   case 1:
		   case 3:
		   case 5:
		   case 7:
		   case 8:
		   case 10:
		   case 12:days = 31 ;break;
		   case 4:
		   case 6:
		   case 9:
		   case 11:days = 30;
		   }
	   }else{
		   if(year%4==0 && year%100!=0 || year%400==0)
			   days = 29;
		   else  days = 28;
	   }
	   return days;
	}
}
